package ch03.demo.proxy;

import ch03.demo.service.Buyer;
import ch03.demo.service.impl.RealBuyer;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.lang.reflect.Proxy;

public class IntermediaryJdkProxyFactory {
    private Logger logger=Logger.getLogger(IntermediaryJdkProxyFactory.class);
    public static <T> T create(Object target){
        IntermediaryInvocationHandler handler=new IntermediaryInvocationHandler();
        handler.setTarget(target);
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),handler);
    }

    @Test
    public void havealookJdkProxy() throws Exception{
        Buyer buyer=IntermediaryJdkProxyFactory.create(new RealBuyer());
        String result=buyer.havealook();
        logger.debug(result);
    }
}
